/**
 * Copyright (c) 2018 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package com.pagecode.gen.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.json.JSONUtil;
import com.pagecode.gen.factory.MongoDBCollectionFactory;
import com.pagecode.gen.service.SysGeneratorService;
import com.pagecode.gen.utils.PageUtils;
import com.pagecode.gen.utils.Query;
import com.pagecode.gen.utils.R;
import com.pagecode.gen.utils.RRException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 代码生成器
 *
 * @author Mark sunlightcs@gmail.com
 */
@Controller
@RequestMapping("/sys/generator")
@Slf4j
public class SysGeneratorController {
	@Autowired
	private SysGeneratorService sysGeneratorService;

	/**
	 * 列表
	 */
	@ResponseBody
	@RequestMapping("/list")
	public R list(@RequestParam Map<String, Object> params){
		PageUtils pageUtil = sysGeneratorService.queryList(new Query(params));

		return R.ok().put("page", pageUtil);
	}

	/**
	 * 生成代码
	 */
	@RequestMapping("/code")
	public void code(String tables, HttpServletResponse response) throws IOException{
		byte[] data = sysGeneratorService.generatorCode(tables.split(","));

		response.reset();
        response.setHeader("Content-Disposition", "attachment; filename=\"renren.zip\"");
        response.addHeader("Content-Length", "" + data.length);
        response.setContentType("application/octet-stream; charset=UTF-8");

        IOUtils.write(data, response.getOutputStream());
	}

	@RequestMapping("/genPageCode")
	public void genPageCode(@RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
		log.info("入参: {}", JSONUtil.toJsonStr(params));
		byte[] data = sysGeneratorService.genPageCode(params);
		response.reset();
		response.setHeader("Content-Disposition", "attachment; filename=\"pagecode.zip\"");
		response.addHeader("Content-Length", "" + data.length);
		response.setContentType("application/octet-stream; charset=UTF-8");
		IOUtils.write(data, response.getOutputStream());
	}

	@ResponseBody
	@RequestMapping("/columns")
	public R columns(@RequestParam Map<String, Object> params){
		if (!params.containsKey(MongoDBCollectionFactory.TABLE_NAME_KEY)) {
			throw new RRException("查询列信息失败：表名不能为空");
		}
		String tableName = Convert.toStr(params.get(MongoDBCollectionFactory.TABLE_NAME_KEY));
		List<Map<String, String>> dataList = sysGeneratorService.queryColumns(tableName);
		if (CollUtil.isEmpty(dataList)) {
			throw new RRException("查询的列信息为空");
		}
		return R.ok().put("result", dataList);
	}

}
